<?php

namespace app\common\model;

use think\Model;


class Asking extends Model
{

    

    //数据库
    protected $connection = 'database';
    // 表名
    protected $name = 'asking';
    
    // 自动写入时间戳字段
    protected $autoWriteTimestamp = 'int';

    // 定义时间戳字段名
    protected $createTime = 'createtime';
    protected $updateTime = 'updatetime';
    protected $deleteTime = false;

    // 追加属性
    protected $append = [
        'publishdata_text',
        'recommenddata_text',
        'hotdata_text',
        'topdata_text',
        'publishtime_text',
        'createtime_text',
        'status_text'
    ];
    

    
    public function getPublishdataList()
    {
        return ['0' => __('Publishdata 0'), '1' => __('Publishdata 1')];
    }

    public function getRecommenddataList()
    {
        return ['0' => __('Recommenddata 0'), '1' => __('Recommenddata 1')];
    }

    public function getHotdataList()
    {
        return ['0' => __('Hotdata 0'), '1' => __('Hotdata 1')];
    }

    public function getTopdataList()
    {
        return ['0' => __('Topdata 0'), '1' => __('Topdata 1')];
    }

    public function getStatusList()
    {
        return ['0' => __('Status 0'), '1' => __('Status 1')];
    }

    public function getPublishdataTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['publishdata']) ? $data['publishdata'] : '');
        $list = $this->getPublishdataList();
        return isset($list[$value]) ? $list[$value] : '';
    }


    public function getRecommenddataTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['recommenddata']) ? $data['recommenddata'] : '');
        $list = $this->getRecommenddataList();
        return isset($list[$value]) ? $list[$value] : '';
    }


    public function getHotdataTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['hotdata']) ? $data['hotdata'] : '');
        $list = $this->getHotdataList();
        return isset($list[$value]) ? $list[$value] : '';
    }


    public function getTopdataTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['topdata']) ? $data['topdata'] : '');
        $list = $this->getTopdataList();
        return isset($list[$value]) ? $list[$value] : '';
    }


    public function getPublishtimeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['publishtime']) ? $data['publishtime'] : '');
        return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
    }

    public function getCreatetimeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['createtime']) ? $data['createtime'] : '');
        return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
    }


    public function getStatusTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['status']) ? $data['status'] : '');
        $list = $this->getStatusList();
        return isset($list[$value]) ? $list[$value] : '';
    }

    protected function setPublishtimeAttr($value)
    {
        return $value === '' ? null : ($value && !is_numeric($value) ? strtotime($value) : $value);
    }

    public function user()
    {
        return $this->belongsTo('User', 'user_id', 'id',[], 'LEFT')->field('id,nickname,username,avatar');
    }

    public function site()
    {
        return $this->belongsTo('User', 'site_id', 'id', [], 'LEFT')->field('id,sitename');
    }

    public function _list($field='*',$where=[],$limit=15,$order='id desc'){
        $ret = $this
            ->with(['user','site'])
            ->field($field)
            ->where($where)
            ->order($order)
            ->paginate($limit);
        return $ret;
    }
}
